#include <QGroupBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QFormLayout>
#include <QColorDialog>
#include <QFontComboBox>
#include <QComboBox>
#include <QFontDatabase>
#include <QList>
#include <QStringList>

#include "biblereaderconfiggeneralpage.h"

BibleReaderConfigGeneralPage::BibleReaderConfigGeneralPage(
        BibleReaderConfigurator *cfg,QWidget *parent) : QWidget(parent)
{
    configurator = cfg;

    colorQSS = QString("background-color: %1");
    QGroupBox *colorGroup = new QGroupBox(tr("Font and Color"));

    QFormLayout *colorLayout = new QFormLayout(this);
    selectedVerseBGBtn = new QPushButton(tr(""));
    selectedVerseBGBtn->setStyleSheet(colorQSS.arg(configurator->getSelectedVerseBG().name()));
    selectedVerseBGBtn->setFixedSize(50, 25);
    projectDlgBGBtn = new QPushButton(tr(""));
    projectDlgBGBtn->setStyleSheet(colorQSS.arg(configurator->getProjectDlgBG().name()));
    projectDlgBGBtn->setFixedSize(50,25);
    projectDlgFGBtn = new QPushButton(tr(""));
    projectDlgFGBtn->setStyleSheet(colorQSS.arg(configurator->getProjectDlgFG().name()));
    projectDlgFGBtn->setFixedSize(50,25);

    // bible font
    fontCB = new QFontComboBox(this);
    fontCB->setCurrentFont(QFont(cfg->getBibleTextFontFamily()));
    fontSizeCB = new QComboBox(this);

    // commonentary font
    cmtFontCB = new QFontComboBox(this);
    cmtFontCB->setCurrentFont(QFont(cfg->getCommTextFontFamily()));
    cmtFontSizeCB = new QComboBox(this);

    // project text font
    prjFontCB = new QFontComboBox(this);
    prjFontCB->setCurrentFont(QFont(cfg->getPrjTextFontFamily()));
    prjFontSizeCB = new QComboBox(this);

    //tip text font
    tipFontCB = new QFontComboBox(this);
    tipFontCB->setCurrentFont(QFont(cfg->getTipTextFontFamily()));
    tipFontSizeCB = new QComboBox(this);

#ifdef Q_OS_OSX
    fontSizeCB->setMinimumWidth(100);
    cmtFontSizeCB->setMinimumWidth(100);
    prjFontSizeCB->setMinimumWidth(100);
    tipFontSizeCB->setMinimumWidth(100);
#endif
    QStringList fontSizesStringList;
    QList<int> fontSizes = QFontDatabase::standardSizes();
    for (int i = 0; i < fontSizes.size(); i++) {
        fontSizesStringList << QString::number(fontSizes[i]);
    }
    fontSizeCB->addItems(fontSizesStringList);
    fontSizeCB->setCurrentText(QString::number((int)cfg->getBibleTextFontSize()));
    cmtFontSizeCB->addItems(fontSizesStringList);
    cmtFontSizeCB->setCurrentText(QString::number((int)cfg->getCommTextFontSize()));
    prjFontSizeCB->addItems(fontSizesStringList);
    prjFontSizeCB->setCurrentText(QString::number((int)cfg->getPrjTextFontSize()));
    tipFontSizeCB->addItems(fontSizesStringList);
    tipFontSizeCB->setCurrentText(QString::number((int)cfg->getPrjTextFontSize()));
    colorLayout->addRow(tr("Selected verse background"), selectedVerseBGBtn);
    colorLayout->addRow(tr("Project background"), projectDlgBGBtn);
    colorLayout->addRow(tr("Project foreground"), projectDlgFGBtn);
    colorLayout->addRow(tr("Project Text Font"), prjFontCB);
    colorLayout->addRow(tr("Project Text Font Size"), prjFontSizeCB);
    colorLayout->addRow(tr("Bible Text Font"), fontCB);
    colorLayout->addRow(tr("Bible Text Font size"), fontSizeCB);
    colorLayout->addRow(tr("Commentary Text Font"), cmtFontCB);
    colorLayout->addRow(tr("Commentary Text Font size"), cmtFontSizeCB);
    colorLayout->addRow(tr("Tip Text Font"), tipFontCB);
    colorLayout->addRow(tr("Tip Text Font size"), tipFontSizeCB);
    colorGroup->setLayout(colorLayout);
    connect(selectedVerseBGBtn, SIGNAL(clicked(bool)), this, SLOT(SVBGBtnClicked()));
    connect(projectDlgBGBtn, SIGNAL(clicked(bool)), this, SLOT(prjBGBtnClicked()));
    connect(projectDlgFGBtn, SIGNAL(clicked(bool)), this, SLOT(prjFGBtnClicked()));
    connect(fontCB, SIGNAL(currentFontChanged(QFont)), this, SLOT(btFontFamilyChanged(QFont)));
    connect(fontSizeCB, SIGNAL(currentIndexChanged(QString)), this, SLOT(btFontSizeChanged(QString)));
    connect(cmtFontCB, SIGNAL(currentFontChanged(QFont)), this, SLOT(cmtFontFamilyChanged(QFont)));
    connect(cmtFontSizeCB, SIGNAL(currentIndexChanged(QString)), this, SLOT(cmtFontSizeChanged(QString)));
    connect(prjFontCB, &QFontComboBox::currentFontChanged, this, &BibleReaderConfigGeneralPage::prjFontFamilyChanged);
    connect(prjFontSizeCB, SIGNAL(currentIndexChanged(QString)), this, SLOT(prjFontSizeChanged(QString)));

    connect(tipFontCB, &QFontComboBox::currentFontChanged, this, &BibleReaderConfigGeneralPage::tipFontFamilyChanged);
    connect(tipFontSizeCB, SIGNAL(currentIndexChanged(QString)), this, SLOT(tipFontSizeChanged(QString)));

    // update group box
    QGroupBox *updateGroup = new QGroupBox(tr("Update"));
    QCheckBox *autoUpdateCheckBox = new QCheckBox(tr("Auto update"));
    if (configurator->getIsAutoUpdate()) {
        autoUpdateCheckBox->setChecked(true);
    }
    connect(autoUpdateCheckBox, SIGNAL(toggled(bool)), this, SLOT(autoCheckBoxClicked(bool)));

    QVBoxLayout *updateLayout = new QVBoxLayout;
    updateLayout->addWidget(autoUpdateCheckBox);
    updateGroup->setLayout(updateLayout);

    // clipboard monitor box
    QGroupBox *monitorGroup = new QGroupBox(tr("Clipboard monitor"));
    QCheckBox *clipboardMonitorCB = new QCheckBox(tr("Clipboard monitor"));
    if (configurator->getEnableClipboardMonitor()) {
        clipboardMonitorCB->setChecked(true);
    }
    connect(clipboardMonitorCB, &QCheckBox::toggled,
            this, &BibleReaderConfigGeneralPage::clipboardMonitorClicked);
    QVBoxLayout *clipboardMonitorLayout = new QVBoxLayout;
    clipboardMonitorLayout->addWidget(clipboardMonitorCB);
    monitorGroup->setLayout(clipboardMonitorLayout);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(colorGroup);
    mainLayout->addWidget(updateGroup);
    mainLayout->addWidget(monitorGroup);
    mainLayout->addSpacing(12);
    mainLayout->addStretch(1);
    setLayout(mainLayout);
}

BibleReaderConfigGeneralPage::~BibleReaderConfigGeneralPage()
{
    delete selectedVerseBGBtn;
    delete projectDlgBGBtn;
    delete projectDlgFGBtn;
}

QColor BibleReaderConfigGeneralPage::getSelectedVerseBG() const
{
    return selectedVerseBG;
}

void BibleReaderConfigGeneralPage::setSelectedVerseBG(const QColor &value)
{
    selectedVerseBG = value;
}

QColor BibleReaderConfigGeneralPage::getPrjBG() const
{
    return prjBG;
}

void BibleReaderConfigGeneralPage::setPrjBG(const QColor &value)
{
    prjBG = value;
}

QColor BibleReaderConfigGeneralPage::getPrjFG() const
{
    return prjFG;
}

void BibleReaderConfigGeneralPage::setPrjFG(const QColor &value)
{
    prjFG = value;
}

void BibleReaderConfigGeneralPage::SVBGBtnClicked()
{
    const QColor color = QColorDialog::getColor(Qt::green, this, tr("Select color"));

    if (color.isValid()) {
        selectedVerseBG = color;
        selectedVerseBGBtn->setStyleSheet(colorQSS.arg(selectedVerseBG.name()));

        configurator->setSelectedVerseBG(color);
    }
}

void BibleReaderConfigGeneralPage::prjBGBtnClicked()
{
    const QColor color = QColorDialog::getColor(Qt::green, this, tr("Select color"));

    if (color.isValid()) {
        prjBG = color;
        projectDlgBGBtn->setStyleSheet(colorQSS.arg(prjBG.name()));

        configurator->setProjectDlgBG(color);
    }
}

void BibleReaderConfigGeneralPage::prjFGBtnClicked()
{
    const QColor color = QColorDialog::getColor(Qt::green, this, tr("Select color"));

    if (color.isValid()) {
        prjFG = color;
        projectDlgFGBtn->setStyleSheet(colorQSS.arg(prjFG.name()));

        configurator->setProjectDlgFG(color);
    }
}

void BibleReaderConfigGeneralPage::autoCheckBoxClicked(bool checked)
{
    if (checked) {
        configurator->setIsAutoUpdate(true);
    } else {
        configurator->setIsAutoUpdate(false);
    }
}

void BibleReaderConfigGeneralPage::clipboardMonitorClicked(bool checked)
{
    if (checked) {
        configurator->setEnableClipboardMonitor(true);
    } else {
        configurator->setEnableClipboardMonitor(false);
    }
}

void BibleReaderConfigGeneralPage::btFontFamilyChanged(QFont f)
{
    configurator->setBibleTextFontFamily(f.family());
}

void BibleReaderConfigGeneralPage::btFontSizeChanged(QString size)
{
    configurator->setBibleTextFontSize(size.toDouble());
}

void BibleReaderConfigGeneralPage::cmtFontFamilyChanged(QFont f)
{
    configurator->setCommTextFontFamily(f.family());
}

void BibleReaderConfigGeneralPage::cmtFontSizeChanged(QString size)
{
    configurator->setCommTextFontSize(size.toDouble());
}

void BibleReaderConfigGeneralPage::prjFontFamilyChanged(QFont f)
{
    configurator->setPrjTextFontFamily(f.family());
}

void BibleReaderConfigGeneralPage::prjFontSizeChanged(QString size)
{
    configurator->setPrjTextFontSize(size.toDouble());
}

void BibleReaderConfigGeneralPage::tipFontFamilyChanged(QFont f)
{
    configurator->setTipTextFontFamily(f.family());
}

void BibleReaderConfigGeneralPage::tipFontSizeChanged(QString size)
{
    configurator->setTipTextFontSize(size.toDouble());
}



